import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
//import './assets/theme/theme-green/index.css'
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
//import NProgress from 'nprogress'
//import 'nprogress/nprogress.css'
import routes from './routes'
/*import Mock from './mock'
Mock.bootstrap();*/
import 'font-awesome/css/font-awesome.min.css'

/*引入axios全局配置*/
import axios from "axios";
axios.defaults.baseURL = "http://localhost:8080/"//配置请求根路径
Vue.prototype.$http = axios
Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)

//NProgress.configure({ showSpinner: false });

const router = new VueRouter({
  routes
})

//1 使用axios前置拦截器，让所有的请求都携带uToken
axios.interceptors.request.use(config=>{
  //从localstorage中获取token
  let token =  localStorage.getItem("token");
  //判断有无token，有就添加到请求头中
  if(token){
    config.headers['U-TOKEN']=token;
  }
  return config;
},error => {
  Promise.reject(error);
})

//2 使用axios后置拦截器，处理没有登录请求
axios.interceptors.response.use(result=>{
  console.log(result.data)
  let data = result.data;
  if(!data.success && data.message==="noLogin")
    router.push({path: "/login"})
  return result;
},error => {
  Promise.reject(error);
})
//3、路由拦截器
router.beforeEach((to, from, next) => {
  //NProgress.start();
  if (to.path == '/login' || to.path == '/shopRegister') {
    localStorage.removeItem('token');
    localStorage.removeItem('logininfo');
    next();
  }else {
    let token = localStorage.getItem('token');
    if (!token && to.path != '/login') {
      next({path: '/login'})
    } else {
      next()
    }
  }
})

new Vue({
  //el: '#app',
  //template: '<App/>',
  router,
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')

